package org.boticelli.auth;

import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.apache.log4j.Logger;
import org.boticelli.dao.UserDAO;
import org.boticelli.model.User;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.dao.DataAccessException;

public class BoticelliUserDetailsService implements UserDetailsService
{
    protected static Logger log = Logger.getLogger(BoticelliUserDetailsService.class);

    private UserDAO userDAO;

    @Required
    public void setUserDAO(UserDAO userDAO)
    {
        this.userDAO = userDAO;
    }

    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException,
            DataAccessException
    {
        User u = userDAO.getUniqueByField("name", name);
        if (u == null)
        {
            throw new UsernameNotFoundException("User "+name+" does not exist.");
        }

        UserDetails ud = new BoticelliUserDetails(u);
        log.debug("USERDETAILS: "+ud);
        return ud;
    }

}
